<?php

class fullCleanTask extends sfBaseTask
{
  protected function configure()
  {

    $this->namespace        = 'wixet';
    $this->name             = 'full-clean';
    $this->briefDescription = 'Reset mysql and ldap database';
    
  }

  protected function execute($arguments = array(), $options = array())
  {
  	
  	if(sfConfig::get('sf_ldap_admin') == ""){
			$settings = sfYaml::load(sfConfig::get('sf_root_dir')."/config/settings.yml");
			$admin = $settings['all']['ldap']['admin'];
			$adminPass = $settings['all']['ldap']['password'];
			$host = $settings['all']['ldap']['hostname'];
			$suffix = $settings['all']['ldap']['suffix'];
		}else{
			$admin = sfConfig::get('sf_ldap_admin');
			$adminPass = sfConfig::get('sf_ldap_password');
			$host = sfConfig::get('sf_ldap_hostname');
			$suffix = sfConfig::get('sf_ldap_suffix');
		}
		
		$ds = ldap_connect($host,10389);
		if(!$ds) throw new Exception('Cant connect to LDAP host "'.$host.'"');
		ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
		
		if(!ldap_bind($ds, $admin,  $adminPass)) throw new Exception('Cant bind to LDAP host "'.$host.'"');
		$sr=ldap_search($ds, $suffix, "uid=*");  
    	$info = ldap_get_entries($ds, $sr);
    	foreach($info as $user){
    		if(strlen($user['uid'][0]) > 0)
	    		if(!ldap_delete($ds, "uid=".$user['uid'][0].",".$suffix))
	    	 		throw new Exception('Error while deleting user "'.$user['uid'][0].'"');
    	}
		$this->log("LDAP purge done");
  		$this->runTask('doctrine:drop-db','--no-confirmation');
  		$this->runTask('doctrine:build-db');
		$this->log("Mysql purge done");
		
  }
}
